package com.scoring.system.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scoring.system.annotation.SkipSecurityCheck;
import com.scoring.system.pojo.Achievement;
import com.scoring.system.pojo.Classes;
import com.scoring.system.pojo.ComprehensiveQuality;
import com.scoring.system.pojo.Course;
import com.scoring.system.pojo.RewardPunishment;
import com.scoring.system.pojo.Student;
import com.scoring.system.pojo.Term;
import com.scoring.system.service.AchievementService;
import com.scoring.system.service.ClassesService;
import com.scoring.system.service.ComprehensiveQualityService;
import com.scoring.system.service.RewardPunishmentService;
import com.scoring.system.service.StudentService;
import com.scoring.system.service.TermSerivce;

@Controller
@RequestMapping("/student")
public class StudentController {
	private static Logger logger = Logger.getLogger(AdminController.class);
	private Map<String, Object> map;
	
	@Autowired
	private StudentService studentService;
    
	@Autowired
	private TermSerivce termSerivce;
	
	@Autowired
	private AchievementService achievementService;
	
	@Autowired
	private ClassesService classesService;
	
	@Autowired
	private RewardPunishmentService rewardService;
	
	@Autowired
	private ComprehensiveQualityService qualityService;

	@RequestMapping("getStudent")
	@SkipSecurityCheck
	@ResponseBody
	public Map getStudent(HttpServletRequest request, ModelMap modelMap) {
		HttpSession se = request.getSession();
		Student student = (Student) se.getAttribute("user");
		logger.info("get student[" + student + "]");
		map = new HashMap();
		map.put("student", student);
		return map;
	}

	@RequestMapping("updateStudentMessage")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateStudnetMeassge(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		Student student = (Student) se.getAttribute("user");
		String studentId = request.getParameter("student_id");
		String studentName = request.getParameter("student_name");
		String mailbox = request.getParameter("mailbox");
		String sex = request.getParameter("sex");
		String phone = request.getParameter("phone");
		logger.info("get StudentMessage" + "studentId=" + studentId + ",studentName=" + studentName + ",mailbox=" + mailbox+
				",sex=" + sex+",phone=" + phone);
		if (!studentId.equals(student.getStudentNumber())) {
			map.put("isSame", "no");
		} else {
			student.setName(studentName);
			student.setMailbox(mailbox);
			student.setGender(sex);
			student.setPhone(phone);
			int count = studentService.updateStudent(student);
			if (count > 0) {
				map.put("update", "true");
			} else {
				map.put("update", "failed");
			}
		}
		return map;
	}

	@RequestMapping("updateStudentPassword")
	@SkipSecurityCheck
	@ResponseBody
	public Map updatePssword(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String oldPassword = request.getParameter("old_password");
		String newPassword = request.getParameter("new_password");
		String confrimPassword = request.getParameter("confirm_password");
		logger.info("get scorePromoterPassword:old_password" + oldPassword + ",new_password=" + newPassword
				+ ",confirm_password=" + confrimPassword);
		HttpSession se = request.getSession();
		Student student = (Student) se.getAttribute("user");
		if (!oldPassword.equals(student.getPassword())) {
			map.put("isExsit", "false");
		} else if (!newPassword.equals(confrimPassword)) {
			map.put("equal", "false");
		} else {
			student.setPassword(newPassword);
			int count = studentService.updateStudent(student);
			if (count > 0) {
				map.put("update", "true");
			} else {
				map.put("update", "false");
			}
		}
		return map;
	}
	@RequestMapping("listTermByClass")
	@SkipSecurityCheck
	@ResponseBody
	public Map listTermByClass(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se = request.getSession();
		Student student =(Student) se.getAttribute("user");
		Classes clazz = classesService.getClassById(student.getClassId());
		String className=clazz.getName();
		int index= className.indexOf("2");
		int year =Integer.valueOf(className.substring(index,index+4));
		List<Term> term =new ArrayList<>();
		List<Term> terms =termSerivce.listTerm();
		for(Term te : terms ){
			logger.info("get term:"+te);
			int year1=Integer.valueOf(te.getName().substring(0, 4));
			logger.info("get year1="+year1);
			if(year-year1==0||year-year1>=-3){
				term.add(te);
			}
		}
		map.put("term", term);
		return map;
	}
	@RequestMapping("listAchievementByStudentAndTerm")
	@SkipSecurityCheck
	@ResponseBody
	public Map listAchievementByStudentAndTerm(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		String term = request.getParameter("term");
		Student student =(Student)se.getAttribute("user");
		List<Achievement> achievement = new ArrayList<>();
		List<Achievement> achievements = new ArrayList<>();
		if("全部学期".equals(term)){
			achievements=achievementService.listAchievementByStudentId(student.getId());
			for (Achievement ach : achievements) {
				logger.info("get achievement:" + ach);
				if (ach.getIsDeleted() != true&&"智".equals(ach.getType())) {
					achievement.add(ach);
				}
			}
		}else{
			achievements=achievementService.listAchievementByStudentIdAndTerm(student.getId(),term);
			for (Achievement ach : achievements) {
				logger.info("get achievement:" + ach);
				if (ach.getIsDeleted() != true) {
					achievement.add(ach);
				}
			}
		}
		map.put("achievement", achievement);
		return map;
	}
	@RequestMapping("listRewardPunishmentByStudent")
	@SkipSecurityCheck
	@ResponseBody
	public Map listRewardPunishmentByStudent(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se =request.getSession();
		Student student =(Student)se.getAttribute("user");
		List<RewardPunishment> rewardPunishment =new ArrayList<>();
		List<RewardPunishment> rewardPunishments =rewardService.listRewardPunishmentByStudentID(student.getId());
		if(rewardPunishments.size()==0){
			map.put("isNull", "yes");
		}else{
			for(RewardPunishment rp : rewardPunishments ){
				logger.info("get rewardPunishment:"+rp);
				if(rp.getIsDeleted()!=true){
					rewardPunishment.add(rp);
				}
			}
			map.put("rewardPunishment", rewardPunishment);
		}
		return map;
	}
	@RequestMapping("listComprehensiveQualityByStudent")
	@SkipSecurityCheck
	@ResponseBody
	public Map listComprehensiveQualityByStudent(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se =request.getSession();
		Student student = (Student)se.getAttribute("user");
		logger.info("get student="+student);
		ComprehensiveQuality comprehensiveQuality=qualityService.getComprehensiveQualityByStudentId(student.getId());
		map.put("comprehensiveQuality", comprehensiveQuality);
		return map;
	}

}
